package com.cmcc.aregister.ui.activity;

import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.constant.ConfigUtilHelper;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class PayWebsite extends ParentActivity implements OnClickListener{
	private final String TAG = "PayWebsite";
	
	private WebView webview;
	private ProgressDialog progressDialog;
//	private Button refreshBtn;
	Thread thread;
	String url;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);  
        setContentView(R.layout.pay_webview); 
//        refreshBtn = (Button) findViewById(R.id.refresh_btn);
//        refreshBtn.setOnClickListener(this);
        webview = (WebView)findViewById(R.id.website_view);  
        url = getIntent().getExtras().getString("url");
        loadUrl();
	}
	private void loadUrl(){
		progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setTitle(getString(R.string.notice));
        progressDialog.setIcon(android.R.drawable.ic_dialog_info);
        progressDialog.setMessage(getString(R.string.loading_web));
		progressDialog.setIndeterminate(true); 
		progressDialog.setCancelable(true);  
        progressDialog.show();
         
        thread = new Thread(new Runnable() {
			@Override
			public void run() {
				webview.getSettings().setJavaScriptEnabled(true);   
		        webview.getSettings().setPluginsEnabled(true);
		        webview.getSettings().setSupportZoom(true);
//		        webview.getSettings().setBuiltInZoomControls(true);
		        //加载url前，设置图片阻塞
//		        webview.getSettings().setBlockNetworkImage( true );
		        webview.loadUrl(url);   
		        MyWebViewClient myWebView = new MyWebViewClient();   
		        webview.setWebViewClient(myWebView);   
			}
		});
        thread.start();
        
	}
	
	@Override//设置回退   
    public boolean onKeyDown(int keyCode, KeyEvent event) {   
        /*if((keyCode== KeyEvent.KEYCODE_BACK) && webview != null && webview.canGoBack()){   
            webview.goBack();   
            return true;   
        }  */ 
        return super.onKeyDown(keyCode, event);   
    }   
	
    private class MyWebViewClient extends WebViewClient{   
        @Override  
        public boolean shouldOverrideUrlLoading(WebView view, String url) {   
            view.loadUrl(url);   
            return true;   
        }   
        @Override  
        public void onPageFinished(WebView view, String url) { 
        	//加载完毕后，关闭图片阻塞
        	webview.getSettings().setBlockNetworkImage(false);
        	progressDialog.dismiss();   
        }   
        @Override  
        public void onReceivedError(WebView view, int errorCode,   
                String description, String failingUrl) {   
            // TODO Auto-generated method stub   
            super.onReceivedError(view, errorCode, description, failingUrl);   
            progressDialog.dismiss();   
        }   
    }  
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Intent intent = new Intent(PayWebsite.this,
				AppManagerActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
		if(ConfigUtilHelper.debug){
			Log.i(TAG,"------onDestroy------");
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		if(ConfigUtilHelper.debug){
			Log.i(TAG,"------onPause------");
		}
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		if(ConfigUtilHelper.debug){
			Log.i(TAG,"------onRestart------");
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if(ConfigUtilHelper.debug){
			Log.i(TAG,"------onResume------");
		}
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		if(ConfigUtilHelper.debug){
			Log.i(TAG,"------onStart------");
		}
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		if(ConfigUtilHelper.debug){
			Log.i(TAG,"------onStop------");
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
//		if(v == refreshBtn){
//			loadUrl();
//		}
	}
	
}
